<template>
  <div class="table">
  <el-table
    :data="tableData.data"
    border
    stripe
    style="width: 100%">
    <template v-for="(item,key) in tableData.columns" >
      <el-table-column
      align="center"
      :key="key"
       v-if="item.type == 'slot'"
      :prop="item.prop"
      :label="item.label"
      :width="item.width">
      <template slot-scope="scope">
        <slot :name="item.slot_name" :row="scope.row"></slot>
      </template>
    </el-table-column>
    <el-table-column
    align="center"
     show-overflow-tooltip
      :key="item.label"
       v-else-if="!item.hide"
      :prop="item.prop"
      :label="item.label"
      :width="item.width">
    </el-table-column>
  </template>
  </el-table>
  <!-- 分页 -->
  <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="tableData.pageData.currentPage"
      :page-sizes="tableData.pageData.pageSizes"
      layout="total, sizes, prev, pager, next, jumper"
      :total="tableData.pageData.total">
    </el-pagination>
</div>
</template>

<script>

export default {
  props: {
    data: {
      type: Object
    }
  },
  watch: {
    data: {
      handler (newValue) {
        console.log('newValue', newValue)
        for (const key in newValue) {
          if (Object.keys(this.tableData).includes(key)) {
            this.tableData[key] = newValue[key]
          }
        }
        console.log('this.tableData', this.tableData)
      },
      immediate: true,
      deep: true
    }
  },
  data () {
    return {
      tableData: {
        columns: [], // 表头
        data: [], // 列表数据
        pageData: {
          pageSizes: [10, 15, 20, 25],
          currentPage: 1,
          total: 0
        } // 分页数据
      }
    }
  },
  created () {
    console.log(this.data)
  },
  methods: {
    handleSizeChange (val) {
      this.$emit('setSizePage', val)
      console.log(`每页 ${val} 条`)
    },
    handleCurrentChange (val) {
      console.log(`子组件当前页: ${val}`)
      this.$emit('setCurrentPage', val)
    }
  }
}
</script>

<style>
.table{
  width: 100%;
}
.table .el-pagination{
  margin-top: 20px;
  float: right;
}
</style>
